/**
 * Created by lixiaodi on 2019/04/10.
 */
var data = [
	{
		id: 123344,
		name: '报销',
		createTime: '2019-04-01'
	}
]
mini.parse();
var tabs1 = mini.get('tabs1');					//tab
var gridTab1 = mini.get('datagrid1');			//待办
var gridTab2 = mini.get('datagrid2');			//已办
var gridTab3 = mini.get('datagrid3');			//历史
var gridTab4 = mini.get('datagrid4');			//全部
var commentGrid = mini.get('commentGrid');      //审核记录

var cTableName;
var waitTask;			//待办
var alreadyTask;		//已办
var historyTask;		//历史
var allTask;			//全部

//初始化
$(function(){
	//allTaskAjax();
	waitHttpList();
	//gridTab1.setData(data);
});
//当tab切换的时候
tabs1.on('beforeactivechanged',function (e){
	var idx = e.index;
	//待办
	if(idx == 0){
		console.log(idx);
		//gridTab1.setData(data);
		waitHttpList();
		//已办
	}else if(idx == 1){
		console.log(idx);
		alreadyHttpList();
		//历史
	}else if(idx == 2){
		console.log(idx);
		historyHttpList();
	}else{
		console.log(idx);
		allTaskAjax()
	}
})
//绘制行内操作按钮(tab1)
function onActionRenderer(e) {
	var grid = e.sender;
	var record = e.record;
	var taskId = record.id;
	var gridId = grid.id;
	//var taskId = record._uid;
	var rowIndex = e.rowIndex;
	//根据数据表显示按钮
	var s;
	if(grid.id == 'datagrid1'){
		s = ' <a class="mini-button Edit_Button" href="javascript:handlingTask(\'' + taskId + ',' + gridId + '\')" >办理任务</a>'
			+ ' <a class="mini-button" iconCls="icon-date" onclick="oldPostil(\'' + taskId + ',' + gridId + '\')">审核记录</a>'
			+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}else if(grid.id == 'datagrid2'){
		s =  ' <a class="mini-button" iconCls="icon-date" onclick="oldPostil(\'' + taskId + ',' + gridId + '\')">审核记录</a>'
			+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}else{
		s =  ' <a class="mini-button" iconCls="icon-date" onclick="oldPostil(\'' + taskId + ',' + gridId + '\')">审核记录</a>'
			+ ' <a class="mini-button Edit_Button" href="javascript:seeImg(\'' + taskId + ',' + gridId + '\')" >查看当前流程图</a>';
	}
	return s;
}
//弹出窗口路径前缀
var url = bootPATH + "../frame/frame1/pages/";
//办理任务
function handlingTask(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	var gridTab1Data = gridTab1.getData();
	var row;
	gridTab1Data.forEach(function(task,index) {
		if(task.id == taskarr[0]){
			row = gridTab1Data[index];
		}
	});
	console.log(row,'row++++++');
	cTableName=row.cTableName;
	//请求表单数据（带入到办理任务页面）
	var obj={
		cTableName: row.cTableName,// 主表的名称
		//cTableName: "baoxiaodan",// 主表的名称
		//formId: row.formlistId,// 表单id
		//formId: 126,// 表单id
		id: row.taskId , //列的id
		//mTableId: '1554861022123'  //列的id
		processInstanceId: row.processId  //列的id
	}
	console.log(obj);
	// 单据数据请求
	$.ajax({
		url: ajaxUrl + "/sqlAction/findById.do",
		type: "POST",
		data: JSON.stringify(obj),
		dataType : 'json',
		contentType : 'application/json',
		success: function (data) {
			var formidd = data[0][cTableName][cTableName+'_id'].split('_')[0];
			formidd = parseInt(formidd);
			localStorage.setItem("formidd",JSON.stringify(formidd));
			var formiddd=localStorage.getItem("formidd");
			//打开办理任务tab
			var taskHandle = {
				action: 'handle',
				taskId: row.taskId,
				id: row.id,
				taskName: row.taskName,
				formListId: row.formlistId,
				cTableName: row.cTableName,
				processInstanceId :row.processId,
				//cTableName: "baoxiaodan",
				//edit_data: data[0][cTableName]
				edit_data: data[0]
			};

			localStorage.removeItem("taskHandle");
			localStorage.setItem("taskHandle",JSON.stringify(taskHandle));
			var formSh = localStorage.getItem("formShow");
			var  formSh=$.parseJSON(formSh);

			//打开tab页面配置信息
			var item = {
				iconCls: "fa fa-desktop",
				text: "审批",
				url: "../pages/activiti/datagridDetail/approvalDetail.html"
			};
			//与父页面通信
			parent.activeTab(item);
		},
		error: function (data) {
			console.log(data);
		}
	});
}

//关闭流程图
function closeWin(){
	console.log("11111111111111111111111");
	var process = document.getElementById("process");
	console.log(process);
	process.style.display = "none";
}
//关闭审核记录
function closecommentWin(){
	console.log("11111111111111111111111");
	var comments = document.getElementById("comments");
	console.log(comments);
	comments.style.display = "none";
}


//流程执行过程
function flowProcess(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	if(gridId == 'datagrid2'){
		var gridTab2Data = gridTab2.getData();
	}else if(gridId == 'datagrid3'){
		var gridTab3Data = gridTab3.getData();
	}
}
//历史批注
function oldPostil(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	var gridTabData;
	if(gridId == 'datagrid1'){
		gridTabData = gridTab1.getData();
	}else if(gridId == 'datagrid2'){
		gridTabData = gridTab2.getData();
	}else if(gridId == 'datagrid3'){
		gridTabData = gridTab3.getData();
	}else if(gridId == 'datagrid4'){
		gridTabData = gridTab4.getData();
	}
	var row;
	gridTabData.forEach(function(task,index){
		if(task.id == taskarr[0]){
		row = gridTabData[index];
		console.log(row);
	}
});

	console.log("===============");
	var taskId = row.processId;
	console.log(taskId);
	if(taskId){
		$.ajax({
			// url: ajaxUrl + "/activitiTaskController/listHistoryComment.do",
			url: ajaxUrl + "/activitiTaskController/listHistoryCommentWithProcessInstanceId.do",
			type:"post",
			data:{
				taskId : taskId
			},
			success: function (data) {
				console.log(data);
				var gridTab1Arr = data.data;
				//时间格式
				for(var i = 0; i < gridTab1Arr.length; i++){
					//日期格式转换
					if(gridTab1Arr[i].createTime){
						gridTab1Arr[i].createTime = formatDate(gridTab1Arr[i].createTime);
					}
				}
				console.log(gridTab1Arr);
				console.log(commentGrid);
				commentGrid.setData(gridTab1Arr);
				var comments = document.getElementById("comments");
				console.log(comments);
				comments.style.display = "block";
			},
			error: function () {
				mini.alert("失败");
			}
		});

	}else{
		alert('当前无审核记录');
	}

}
//查看流程图
function seeImg(task){
	var taskarr = task.split(',');
	var gridId = taskarr[1];
	var gridTabData;
	if(gridId == 'datagrid1'){
		gridTabData = gridTab1.getData();
	}else if(gridId == 'datagrid2'){
		gridTabData = gridTab2.getData();
	}else if(gridId == 'datagrid3'){
		gridTabData = gridTab3.getData();
	}else if(gridId == 'datagrid4'){
		gridTabData = gridTab4.getData();
	}
	var row;
	gridTabData.forEach(function(task,index){
		if(task.id == taskarr[0]){
		row = gridTabData[index];
	}
});
	var row;

	//得到被操作行数据
	console.log(row)
	//var row = gridTab1.getRowByUID(row_uid);
	var taskId = row.processId;
	console.log(taskId);
	if (taskId) {
		fileUrl = ajaxUrl + "/activitiTaskController/getView.do?processInstanceId=" + taskId;
		images = document.getElementById("imgs")
		images.src = fileUrl;
		var process = document.getElementById("process");
		console.log(process);
		process.style.display = "block";
	}else{
		mini.alert('当前无流程');
	}






	/* $.ajax({
         url: ajaxUrl + "/activitiTaskController/getView.do?processInstanceId=" + taskId ,
         type:"post",
         success: function (data) {
             console.log(data);
             //grid.setData(data.data)
             //grid.reload();
         },
         error: function () {
             mini.alert("失败");
         }
     });*/
}
//待办刷新
function waitRefresh(){
	waitHttpList();
}
//已办刷新
function alreadyRefresh(){
	alreadyHttpList();
}
//历史刷新
function historyRefresh(){
	historyHttpList();
}
//待办list请求
function waitHttpList(){
	$.ajax({
		url: ajaxUrl + "/activitiTaskController/taskPage.do",
		type:"post",
		success: function (data) {
			console.log(data);
			var gridTab1Arr = data.data;
			//时间格式
			for(var i = 0; i < gridTab1Arr.length; i++){
				//日期格式转换
				if(gridTab1Arr[i].createTime){
					gridTab1Arr[i].createTime = formatDate(gridTab1Arr[i].createTime);
				}
			}
			gridTab1.setData(gridTab1Arr);
			//grid.reload();
		},
		error: function () {
			mini.alert("失败");
		}
	});
}
//已办list请求
function alreadyHttpList(){
	$.ajax({
		url: ajaxUrl + "/activitiTaskHisController/taskUnfinishPage.do",
		type:"post",
		success: function (data) {
			console.log(data);
			var datagrid3Arr = data.MyTaskList;
			//时间格式
			for(var i = 0; i < datagrid3Arr.length; i++){
				//日期格式转换
				if(datagrid3Arr[i].createTime){
					datagrid3Arr[i].createTime = formatDate(parseInt(datagrid3Arr[i].createTime));
				}
			}
			gridTab2.setData(datagrid3Arr);
		},
		error: function () {
			mini.alert("失败");
		}
	});
}
//历史list请求（已完成）
function historyHttpList(){
	$.ajax({
		url: ajaxUrl + "/activitiTaskHisController/taskHisPage.do",
		type:"post",
		success: function (data) {
			console.log(data);
			var datagrid3Arr = data.MyTaskList;
			console.log(datagrid3Arr);
			//时间格式
			for(var i = 0; i < datagrid3Arr.length; i++){
				//日期格式转换
				if(datagrid3Arr[i].createTime){
					datagrid3Arr[i].createTime = formatDate(parseInt(datagrid3Arr[i].createTime));
				}
			}
			gridTab3.setData(datagrid3Arr);
		},
		error: function () {
			mini.alert("失败");
		}
	});
}
//全部任务请求
function allTaskAjax(){
	$.ajax({
		url: ajaxUrl + "/activitiTaskHisController/taskAllPage.do",
		type:"post",
		success: function (data) {
			console.log(data);
			var datagrid4Arr = data.MyTaskList;
			//时间格式
			for(var i = 0; i < datagrid4Arr.length; i++){
				//日期格式转换
				if(datagrid4Arr[i].createTime){
					datagrid4Arr[i].createTime = formatDate(parseInt(datagrid4Arr[i].createTime));
				}
			}
			gridTab4.setData(datagrid4Arr);
			//grid.reload();
		},
		error: function () {
			mini.alert("失败");
		}
	});



}